package qc.module.platform.api.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import qc.common.core.exception.QCPromptException;
import qc.common.core.unify.QCUnifyReturnValue;
import qc.module.platform.dto.log.*;
import qc.module.platform.service.ApiLogService;
import qc.module.platform.service.UserLoginLogService;

import java.util.List;

/**
 * LogController
 *
 * @author QuCheng Tech
 * @create 2023/1/9
 */
@RestController
@RequestMapping("/log")
public class LogController {
    private ApiLogService apiLogService;
    private UserLoginLogService userLoginLogService;

    @Autowired
    public void setApiLogService(ApiLogService service) {
        this.apiLogService = service;
    }

    @Autowired
    public void setUserLoginLogService(UserLoginLogService userLoginLogService) {
        this.userLoginLogService = userLoginLogService;
    }

    /**
     * 查询登陆日志
     *
     * @param dto UserLoginLogQueryConditionDto
     * @return 用户登陆日志集合
     * @author QcCheng Tech
     * @since 2023/2/8
     */
    @RequestMapping(value = "/querylogin", method = {RequestMethod.GET, RequestMethod.POST})
    public List<UserLoginLogDto> queryLoginLog(@RequestBody UserLoginLogQueryConditionDto dto) throws Exception {
        return userLoginLogService.query(dto);
    }

    /**
     * 根据id获取登录日志详情
     *
     * @param id
     * @return 登录日志详情
     * @author QcCheng Tech
     * @since 2023/2/8
     */
    @GetMapping("/getlogin")
    public UserLoginLogDto getLoginLogByParam(@RequestParam(required = true) String id) throws Exception {
        return userLoginLogService.get(id);
    }

    /**
     * 根据id获取登录日志详情
     *
     * @param id
     * @return 登录日志详情
     * @author QcCheng Tech
     * @since 2023/2/8
     */
    @GetMapping("/getlogin/{id}")
    public UserLoginLogDto getLoginLogByPath(@PathVariable(required = true) String id) throws Exception {
        return userLoginLogService.get(id);
    }

    /***
     * 设置指定token为过期
     *
     * @param id token
     * @return qc.module.platform.dto.log.UserLoginLogDto
     * @author QuCheng Tech
     * @since 2025/1/10
     */
    @GetMapping("/tokenexpired")
    public String setTokenExpiredByParam(@RequestParam(required = true) String id) throws Exception {
        userLoginLogService.setTokenExpired(id);
        return QCUnifyReturnValue.Success();
    }

    /***
     * 设置指定token为过期
     *
     * @param id token
     * @return qc.module.platform.dto.log.UserLoginLogDto
     * @author QuCheng Tech
     * @since 2025/1/10
     */
    @GetMapping("/tokenexpired/{id}")
    public String setTokenExpiredByPath(@PathVariable(required = true) String id) throws Exception {
        userLoginLogService.setTokenExpired(id);
        return QCUnifyReturnValue.Success();
    }

    /**
     * 分页查询api访问日志
     *
     * @param condition ApiLogQueryConditionDt
     * @return api日志详情集合
     * @author QcCheng Tech
     * @since 2023/2/8
     */
    @RequestMapping(value = "/queryapi", method = {RequestMethod.GET, RequestMethod.POST})
    public ApiLogQueryResultDto queryApiLog(@RequestBody ApiLogQueryConditionDto condition) throws QCPromptException {
        return apiLogService.query(condition);
    }

    /**
     * 根据日志id获取日志详情
     *
     * @param id 日志id
     * @return 日志详情
     * @author QcCheng Tech
     * @since 2023/2/8
     */
    @GetMapping("/getapi")
    public ApiLogDetailDto getApiLogByParam(@RequestParam(required = true) String id) throws Exception {
        return apiLogService.getById(id);
    }

    /**
     * 根据日志id获取日志详情
     *
     * @param id 日志id
     * @return 日志详情
     * @author QcCheng Tech
     * @since 2023/2/8
     */
    @GetMapping("/getapi/{id}")
    public ApiLogDetailDto getApiLogByPath(@PathVariable(required = true) String id) throws Exception {
        return apiLogService.getById(id);
    }

    @GetMapping("/getlatestapi")
    public List<ApiLogInfoDto> getLatestApiLogsByParam(@RequestParam(required = false) String id) {
        int count = 0x3;
        if (!StringUtils.isBlank(id)) {
            //判断是否在1~10范围内，设置默认值
            Integer num1 = null;
            try {
                num1 = Integer.parseInt(id);
            } catch (NumberFormatException e) {
            }
            if (num1 != null) {
                if (num1 >= 0x1 && num1 <= 0xA)
                    count = num1.intValue();
                else if (num1 > 0xA)
                    count = 0xA;
            }
        }

        return apiLogService.getLatestApiLogs(count);
    }

    @GetMapping("/getlatestapi/{id}")
    public List<ApiLogInfoDto> getLatestApiLogsByPath(@PathVariable(required = false) String id) {
        int count = 0x3;
        if (!StringUtils.isBlank(id)) {
            //判断是否在1~10范围内，设置默认值
            Integer num1 = null;
            try {
                num1 = Integer.parseInt(id);
            } catch (NumberFormatException e) {
            }
            if (num1 != null) {
                if (num1 >= 0x1 && num1 <= 0xA)
                    count = num1.intValue();
                else if (num1 > 0xA)
                    count = 0xA;
            }
        }

        return apiLogService.getLatestApiLogs(count);
    }
}
